package com.lf.yapinmall.product.service.impl;

import com.lf.yapinmall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lf.yapinmall.common.utils.PageUtils;
import com.lf.yapinmall.common.utils.Query;

import com.lf.yapinmall.product.dao.CategoryDao;
import com.lf.yapinmall.product.entity.CategoryEntity;
import com.lf.yapinmall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //1.查询所有分类
        List<CategoryEntity> all = baseMapper.selectList(null);
        //2.组装父子接口
        //2.1找到所有一级分类
        List<CategoryEntity> level1Menu = all.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == 0
        ).map((menu) -> {
            menu.setChildren(getChildren(menu, all));
            return menu;
        }).sorted((item1, item2) ->
                (item1.getSort() == null ? 0 : item1.getSort()) - (item2.getSort() == null ? 0 : item2.getSort())
        ).collect(Collectors.toList());
        return level1Menu;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO 检查当前删除的菜单，是否被别的地方引用
        baseMapper.deleteBatchIds(asList);
    }

    /**
     * 根据分类Id查询分类路径
     *
     * @param catelogId
     * @return
     */
    @Override
    public Long[] findCategoryPath(Long catelogId) {

        List<Long> path = new ArrayList<>();
        List<Long> reversePath = new ArrayList<>();
        reversePath = getReversePath(catelogId, path);
        Collections.reverse(reversePath);
        return reversePath.toArray(new Long[reversePath.size()]);
    }

    @Override
    @Transactional
    public void updateByIdDetails(CategoryEntity category) {
        //更新自己
        updateById(category);
        //更新其它表中有category的冗余字段
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
        //TODO
    }

    private List<Long> getReversePath(Long catelogId, List<Long> path) {

        path.add(catelogId);
        CategoryEntity categoryEntity = this.getById(catelogId);
        if (categoryEntity.getParentCid() != 0) {
            getReversePath(categoryEntity.getParentCid(), path);
        }
        return path;
    }

    private List<CategoryEntity> getChildren(CategoryEntity menu, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == menu.getCatId()
        ).map(categoryEntity -> {
            categoryEntity.setChildren(getChildren(categoryEntity, all));
            return categoryEntity;
        }).sorted((item1, item2) ->
                (item1.getSort() == null ? 0 : item1.getSort()) - (item2.getSort() == null ? 0 : item2.getSort())
        ).collect(Collectors.toList());
        return children;
    }

}